package com.ayoka.parenting.ui.meditation.question;

import com.ayoka.parenting.Constants;
import com.ayoka.parenting.bean.BaseResponse;
import com.ayoka.parenting.bean.MeditationQuestionBean;
import com.ayoka.parenting.bean.ResponseMeditationQuestion;
import com.ayoka.parenting.http.API;
import com.ayoka.parenting.http.HttpBeanInterceptCallback;
import com.ayoka.parenting.interf.CallbackUtil;
import com.ayoka.parenting.interf.RequestCallback;
import com.ayoka.parenting.mvp.BasePresenter;

import java.util.List;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: MeditationQuestionModel&.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class MeditationQuestionModel implements MeditationQuestionContract.Model{

    @Override
    public void getMeditationQuestionPresenter(final BasePresenter presenter, final String recordId, final RequestCallback<ResponseMeditationQuestion> callback) {
        API.getMeditationQuestionPresenter(presenter, recordId, new HttpBeanInterceptCallback<BaseResponse<ResponseMeditationQuestion>>() {
            @Override
            public void onSuccess(BaseResponse<ResponseMeditationQuestion> response) {
                CallbackUtil.notifySuccess(callback, response.data);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    getMeditationQuestionPresenter(presenter, recordId, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }

    @Override
    public void saveQuestionAnswer(final BasePresenter presenter, final String recordId, final List<MeditationQuestionBean.AnswerResultBean> answerResult, final RequestCallback<BaseResponse> callback) {
        API.saveQuestionAnswer(presenter, recordId, answerResult, new HttpBeanInterceptCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                CallbackUtil.notifySuccess(callback, response);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    saveQuestionAnswer(presenter, recordId, answerResult, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }

    @Override
    public void synchronizedCircleGroup(final BasePresenter presenter, final String content, final RequestCallback<BaseResponse> callback) {
        API.synchronizedCircleGroup(presenter, content, new HttpBeanInterceptCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                CallbackUtil.notifySuccess(callback, response);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    synchronizedCircleGroup(presenter, content, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }
}
